package com.jy.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jy.common.enums.ResponseWrapper;
import com.jy.entity.GoodCategory;
import com.jy.entity.Goods;
import com.jy.mapper.GoodCategoryMapper;
import com.jy.mapper.GoodsMapper;
import com.jy.service.GoodCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author fzg
 * @since 2024-12-21
 */
@Service
public class GoodCategoryServiceImpl extends ServiceImpl<GoodCategoryMapper, GoodCategory> implements GoodCategoryService {

    private final GoodCategoryMapper goodCategoryMapper;
    private final GoodsMapper goodsMapper;

    public GoodCategoryServiceImpl(GoodCategoryMapper goodCategoryMapper, GoodsMapper goodsMapper) {
        this.goodCategoryMapper = goodCategoryMapper;
        this.goodsMapper = goodsMapper;
    }

    @Override
    public ResponseWrapper getGoodsCategoryList() {

        // 左边分类列表
        List<GoodCategory> goodCategories = goodCategoryMapper.selectList(new LambdaQueryWrapper<GoodCategory>()
                .isNull(GoodCategory::getParentAid)
        );
        return ResponseWrapper.markSuccess(goodCategories);

    }

    @Override
    public ResponseWrapper getGoodByCategoryAid(Integer categoryAid) {

        // 返回结果
        ArrayList<HashMap<String, Object>> result = new ArrayList<>();
        List<GoodCategory> goodCategories = goodCategoryMapper.selectList(new LambdaQueryWrapper<GoodCategory>()
                .eq(GoodCategory::getParentAid, categoryAid)
        );
        if (goodCategories.size() > 0){
            for (GoodCategory goodCategory : goodCategories) {
                HashMap<String, Object> category = new HashMap<>();
                category.put("categoryAid",goodCategory.getAid());
                category.put("categoryName",goodCategory.getCategoryName());
                ArrayList<HashMap<String, Object>> goodLists = new ArrayList<>();
                List<Goods> goods = goodsMapper.selectList(new LambdaQueryWrapper<Goods>()
                        .eq(Goods::getGoodTypeId, goodCategory.getAid())
                );
                if (goods.size() > 0){
                    for (Goods good : goods) {
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("goodsAid",good.getAid());
                        map.put("goodsImg",good.getGoodsMainImage());
                        map.put("goodsName",good.getGoodsName());
                        goodLists.add(map);
                    }
                }
                category.put("goodLists",goodLists);
                result.add(category);
            }
        }
        System.out.println(JSONUtil.toJsonStr(result));
        return ResponseWrapper.markSuccess(result);
    }


    public ResponseWrapper getGoodsCategoryList1() {

        // 返回结果
        ArrayList<HashMap<String, Object>> result = new ArrayList<>();

        // 查询所有大类
        List<GoodCategory> goodCategories = goodCategoryMapper.selectList(new LambdaQueryWrapper<GoodCategory>()
                .isNull(GoodCategory::getParentAid)
        );

        for (GoodCategory goodCategory : goodCategories) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("categoryId", goodCategory.getAid());
            map.put("categoryName", goodCategory.getCategoryName().length() > 5 ? goodCategory.getCategoryName().substring(0,5) : goodCategory.getCategoryName());

            List<GoodCategory> goodCategories1 = goodCategoryMapper.selectList(new LambdaQueryWrapper<GoodCategory>()
                    .eq(GoodCategory::getParentAid, goodCategory.getAid())
            );

            ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
            if (goodCategories1.size() > 0) {
                for (GoodCategory category : goodCategories1) {
                    HashMap<String, Object> map1 = new HashMap<>();
                    List<Goods> goods = goodsMapper.selectList(new LambdaQueryWrapper<Goods>()
                            .eq(Goods::getGoodTypeId, category.getAid()));
                    for (Goods good : goods) {
                        map1.put("goodsId", good.getAid());
                        map1.put("goodName", good.getGoodsName().length() > 6 ? good.getGoodsName().substring(0,6) : good.getGoodsName());
                        map1.put("goodImage", good.getGoodsMainImage());
                        map1.put("parentId",category.getAid());
                        map1.put("parentName",category.getCategoryName());
                        hashMaps.add(map1);
                    }
                }

            } else {
                List<Goods> goods = goodsMapper.selectList(new LambdaQueryWrapper<Goods>()
                        .eq(Goods::getGoodTypeId, goodCategory.getAid()));

                for (Goods good : goods) {
                    HashMap<String, Object> map1 = new HashMap<>();
                    map1.put("goodsId", good.getAid());
                    map1.put("goodName", good.getGoodsName().length() > 6 ? good.getGoodsName().substring(0,6) : good.getGoodsName());
                    map1.put("goodImage", good.getGoodsMainImage());
                    map1.put("parentId",null);
                    map1.put("parentName",null);
                    hashMaps.add(map1);
                }
            }
            map.put("catagoryGoods",hashMaps);
            result.add(map);
        }

        return ResponseWrapper.markSuccess(result);
    }
}
